// pages/homePage/index.js
const app = getApp()
var sliderWidth = 96;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    house: null,
    courseList: null,
    courseList3: null,

    tabs: [],
    activeIndex: 0,
    sliderOffset: 0,
  },

  swiperClick(e) {
    console.log(e);
    let obj = e.currentTarget.dataset;

    wx.navigateTo({
      url: "./showDetail/index?adId=" + obj.id
    })
  },

  showDetail(e) {
    console.log(e);
    let obj = e.currentTarget.dataset.obj;

    wx.navigateTo({
      url: "./showDetail/index?course=" + encodeURIComponent(JSON.stringify(obj))
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log("homePage, 读token: ", JSON.stringify(app.globalData.token));

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });

    wx.request({
      url: app.globalData.serverUrl + "/house/getById",
      header: {
        token: app.globalData.token
      },
      success(resp) {
        console.log("homePage, 读企业信息: ", JSON.stringify(resp));
        if(resp.data.success) {
          that.setData({
            house: resp.data.data
          });

          app.globalData.parentEnterpriseName = resp.data.data.name;
          
          // wx.setNavigationBarTitle({
          //   title: app.globalData.parentEnterpriseName
          // });
        }
        
      },
      fail(resp) {
        console.error(resp);
      }
    });

    // 课程类型
    wx.request({
      url: app.globalData.serverUrl + "/valuelist/queryList",
      header: {
        token: app.globalData.token
      },
      data: {
        condition: {
          parentCode: 'courseType'
        }
      },
      success(resp) {
        console.log(resp);
        if (resp.data.success) {
          that.setData({
            tabs: resp.data.data
          });
        }
      },
      fail(resp) {
        console.error(resp);
      }
    });

    // 课程列表
    wx.request({
      url: app.globalData.serverUrl + "/course/queryCourseListByType",
      header: {
        token: app.globalData.token
      },
      data: {
        type: 'courseType_1'
      },
      success(resp) {
        console.log(resp);
        if (resp.data.success) {
          that.setData({
            courseList: resp.data.data
          });
        }
      },
      fail(resp) {
        console.error(resp);
      }
    });

    wx.request({
      url: app.globalData.serverUrl + "/course/queryCourseListByType",
      header: {
        token: app.globalData.token
      },
      data: {
        type: 'courseType_3'
      },
      success(resp) {
        console.log(resp);
        if (resp.data.success) {
          that.setData({
            courseList3: resp.data.data
          });
        }
      },
      fail(resp) {
        console.error(resp);
      }
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
    return {
      title: "约课",
      imageUrl: "https://cw1.tw/CH/images/channel_master/af164017-3505-4e04-b1b3-bb45cd10cb94.jpg",
      path: "/pages/index/index?enterpriseId=" + app.globalData.enterpriseId,
      success: function (res) {
        var shareTickets = res.shareTickets;
        if (shareTickets.length == 0) {
          return false;
        }
        wx.getShareInfo({
          shareTicket: shareTickets[0],
          success: function (res) {
            var encryptedData = res.encryptedData;
            var iv = res.iv;
          }
        })
      },
      fail: function (res) {
        // 转发失败
        console.log(res);
      }
    }
  },

  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  }
})